﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyDes;
public partial class single_singlelgn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string zxdstr;  //接收来自JYHD的字符串，要求形如： JYHD+用户名
            zxdstr  = Request.QueryString["zid"];
            if(string.IsNullOrEmpty(zxdstr))
            {
                Response.Redirect("../index.aspx",true);
            }
            zxdstr = zxdstr.Replace(",07baby01,", "#");
            zxdstr = zxdstr.Replace(",07baby02,", "&");
            zxdstr = zxdstr.Replace(",07baby03,", "+");
           
            My_Des zxdDes = new My_Des();
            zxdDes.DecryptStr = zxdstr;
            zxdDes.MyDesKey = "tiger11!";
            zxdDes.DesDecrypt();
            zxdstr = zxdDes.MyDesStr;
            if (zxdstr == null)
                Response.Redirect("~/index.aspx",true);

            

            if (zxdstr.Substring(0,4)!="JYHD")
            {
                Response.Write("<script language=javascript>alert('请从指定网站调用，页面即将关闭。。。。');if(opener!=null){opener.location.reload();window.close();}</script>");
                //Response.Write("< script>window.close();");// 会弹出询问是否关闭
                Response.Write("< script>window.opener=null;window.close();< /script>");// 不会弹出询问
            }
            txtJyhdUser.Text=zxdstr.Substring(4);   //取得JYHD传递过来的用户名
            Conn zconn = new Conn();
            string zsql = "select username,pwd from tb_single_login where jyhdusername='" + txtJyhdUser.Text.Trim() + "'";
            string zresult = zconn.sqlTwo(zsql);
            zconn.Dispose();
            if (zresult == "0")   //没找到相关记录，首次尝试单点登录，若存储的用户名密码不正确，也会进入登陆界面
            {
            }
            else         //找到记录。
            {
                if (Membership.ValidateUser(zresult.Substring(0, zresult.IndexOf("|")), zresult.Substring(zresult.IndexOf("|") + 1)))
                {
                    FormsAuthentication.RedirectFromLoginPage(zresult.Substring(0, zresult.IndexOf("|")), true);
                    
                    string zxdpurl0 = "../main_temp.aspx";
                    Response.Redirect(zxdpurl0, true);

                }   
                //这里不设else， 因为只要没转入其他页面，随即将会进入登陆界面
            }
        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
        if (Membership.ValidateUser(txtusrname.Text.Trim(), txtpwd.Text.Trim()))
        {
            //若通过密码验证，将下列3个textbox值存入数据表tb_single_login中
            //txtJyhdUser.Text;
            //txtusrname.Text;
            //txtpwd.Text;
            Conn zconn = new Conn();
            string zsql = "select username,pwd from tb_single_login where jyhdusername='" + txtJyhdUser.Text.Trim() + "'";
            string zresult = zconn.sqlTwo(zsql);
            if (zresult == "0")   //没有记录
            {
                zsql = "insert into tb_single_login (jyhdusername,username,pwd,xtsj1) values('"+txtJyhdUser.Text+"','"+txtusrname.Text+"','"+txtpwd.Text+"','"+DateTime.Now.ToString()+"')";
            }
            else     //有记录
            {
                zsql = "update tb_single_login set jyhdusername='"+txtJyhdUser.Text+"',username='"+txtusrname.Text+"',pwd='"+txtpwd.Text+"',xtsj2='"+DateTime.Now.ToString()+"' where jyhdusername='"+txtJyhdUser.Text+"'";
            }
            int zrelt=zconn.ExeSql(zsql);
            zconn.Dispose();
            FormsAuthentication.RedirectFromLoginPage(txtusrname.Text, true);
            //来自:http://www.2ed.cn/Article-111-15374.shtml
            string zxdpurl0 = "../main_temp.aspx";
            Response.Redirect(zxdpurl0);

        }
    }
}